﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using UnityEngine;

namespace Assets.Scripts.DL.ComponentScripts {
	public class BarrageBall : MonoBehaviour {
		[SerializeField] int Damage;
		[SerializeField] Animator Animator;
		public Vector2 Velocity { get; set; }
		bool _isBoomStart = false;
		private void OnEnable() {
			_isBoomStart = false;
			Animator.Play("Fly");
		}
		private void Update() {
			transform.position += (Vector3)Velocity * Time.deltaTime;
		}

		private void OnTriggerEnter2D(Collider2D collision) {
			if (_isBoomStart)
				return;
			if (collision.CompareTag(Tags.Player)) {
				collision.GetComponent<PlayerController>().TakeDamage(Damage);
			}
			_isBoomStart = true;
			StartCoroutine(Boom());
		}

		IEnumerator Boom() {
			Animator.Play("Boom");
			yield return null;
			while (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.9f) {
				yield return null;
			}
			ObjectPool.Instance.RecycleObject(ObjPoolStr.BarrageBall, gameObject);
		}
	}
}
